﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Data;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.AdConfigManager.ViewModels;
using WFramework;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;

namespace MiniWeb.Web.Controllers.Admin.AdConfigManager
{

    public class AdConfigManagerController : BaseController
    {
        private readonly AdConfigTask _adconfigTask = new AdConfigTask();

        public ActionResult List(int? pid, int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _adconfigTask.GetPagedList(pid,pageIndex, pageSize);

            ViewData["typelist"] = AdPositionEnum.商城推荐.ToSelectListForAll();


            var model = new PagedList<AdConfig>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {

            ViewData["typelist"] = AdPositionEnum.商城推荐.ToSelectList();
            var model = new SaveAdConfig();
            
            if (id != null)
            {
                var item = _adconfigTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<AdConfig, SaveAdConfig>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveAdConfig savemodel)
        {
            ViewData["typelist"] = AdPositionEnum.商城推荐.ToSelectList();

            if (ModelState.IsValid)
            {
                string errMsg, savePath = string.Empty;
                var dict = new Dictionary<string, string>();
                if (savemodel.ID == null)
                {
                    foreach (var key in Request.Files.AllKeys)
                    {
                        if (Request.Files[key] == null || Request.Files[key].ContentLength == 0)
                        {
                            ModelState.AddModelError(key, "请上传");
                            return View(savemodel);
                        }

                        if (!UploadService.UploadFile(Request.Files[key], out savePath, out errMsg))
                        {
                            return AlertMsg(errMsg, HttpContext.Request.UrlReferrer.PathAndQuery);
                        }
                        dict.Add(key, savePath);
                    }

                    var model = new AdConfig
                                    {
                                        ImgPath = dict["ImgPath"],
                                        Title = savemodel.Title,
                                        Sort = savemodel.Sort,
                                        CreateDate = DateTime.Now,
                                        LinkUrl = savemodel.LinkUrl,
                                        Position = savemodel.Position
                                    };
                    _adconfigTask.Add(model);
                }
                else
                {
                    var model = _adconfigTask.GetById((int)savemodel.ID);
                    foreach (var key in Request.Files.AllKeys.Where(key => Request.Files[key] != null && Request.Files[key].ContentLength > 0))
                    {

                        if (!UploadService.UploadFile(Request.Files[key], out savePath, out errMsg))
                        {
                            return AlertMsg(errMsg, HttpContext.Request.UrlReferrer.PathAndQuery);
                        }
                        dict.Add(key, savePath);
                    }

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.ImgPath = dict.ContainsKey("ImgPath") ? dict["ImgPath"] : model.ImgPath;
                    model.Title = savemodel.Title;
                    model.Sort = savemodel.Sort;
                    model.LinkUrl = savemodel.LinkUrl;
                    model.Position = savemodel.Position;
                    _adconfigTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);
            }
            return View(savemodel);
        }



        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _adconfigTask.Delete(id);
        }

        #endregion
    }
}